﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using AviationSimulator.Factories;
using AviationSimulator.Interfaces;
using AviationSimulator.Properties;

namespace AviationSimulator.DataItems.Mode.Receive
{
    public class ReceiveMode : ModeBase, IModeSerializable
    {
        public DataTable ReceivedPackagesDataTable;
        public DataTable PackageDetailsDataTable;

        private List<Package> _packages;

        public ReceiveMode()
        {
            ReceivedPackagesDataTable = new DataTable();
            ReceivedPackagesDataTable.Columns.Add("Package #");
            ReceivedPackagesDataTable.Columns.Add("Time");

            PackageDetailsDataTable = new DataTable();
            PackageDetailsDataTable.Columns.Add("Parameter");
            PackageDetailsDataTable.Columns.Add("Value");

            _packages = new List<Package>();
        }

        public void Serialize(string filePath)
        {
            var modeToSerialize = new ReceiveModeSerializable()
            {
                Address = Address,
                Port = Port
            };

            var serializer = new XmlSerializer(typeof(ReceiveModeSerializable));


            using (var streamWriter = new StreamWriter(filePath))
            {
                serializer.Serialize(streamWriter, modeToSerialize);
            }
        }

        public void Deserialize(string filePath)
        {
            var deserializer = new XmlSerializer(typeof(ReceiveModeSerializable));
            ReceiveModeSerializable modeToDeserialize;

            try
            {
                using (var streamReader = new StreamReader(filePath))
                {
                    modeToDeserialize = (ReceiveModeSerializable)deserializer.Deserialize(streamReader);
                }
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Resources.CantLoadModeParameters);
                return;
            }

            if (modeToDeserialize == null) return;

            Address = modeToDeserialize.Address;
            Port = modeToDeserialize.Port;
        }

        public void LoadPackagesFromTemp()
        {
            _packages = ManagerFactory.PackageManager.TemporaryPackageStorage;

            GeneratePackagesDataTable();
        }

        private void GeneratePackagesDataTable()
        {
            ReceivedPackagesDataTable.Clear();

            foreach (var package in _packages)
            {
                var row = ReceivedPackagesDataTable.NewRow();

                row[0] = package.Number;
                row[1] = package.TimeStamp;

                ReceivedPackagesDataTable.Rows.Add(row);
            }
        }

        public void UploadPackageParameters(int packageIndex)
        {
            var packageNumber = Convert.ToInt32(ReceivedPackagesDataTable.Rows[packageIndex][0]);
            var packageToDisplay = _packages.Single(package => package.Number == packageNumber);

            PackageDetailsDataTable.Clear();

            foreach (var parameter in packageToDisplay.Parameters)
            {
                var row = PackageDetailsDataTable.NewRow();
                row[0] = parameter.Name;
                row[1] = parameter.Value.ToString();
                PackageDetailsDataTable.Rows.Add(row);
            }
        }
    }
}
